#!/bin/bash
  # scripts/gen-proto.sh

  set -e

  # 安装必要的工具
  go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
  go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

  # 定义proto文件目录
  PROTO_DIRS=("auth" "resource" "vm" "nodeproxy")

  ROOT_DIR=$(cd "$(dirname "$0")/.." && pwd)

  echo "Generating protobuf files..."

  for dir in "${PROTO_DIRS[@]}"; do
      echo "Processing $dir..."

      # 检查proto文件是否存在
      if ls ${dir}/*.proto 1> /dev/null 2>&1; then
          # 生成pb.go文件
          protoc \
              -I "${ROOT_DIR}" \
              --go_out="${ROOT_DIR}" \
              --go_opt=paths=source_relative \
              --go-grpc_out="${ROOT_DIR}" \
              --go-grpc_opt=paths=source_relative \
              ${ROOT_DIR}/${dir}/*.proto

          echo "Generated protobuf files for $dir"
      else
          echo "No proto files found in $dir"
      fi
  done

  echo "All protobuf files generated successfully!"
